package com.diyunapp.xilinapp.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.ReceiveBean;
import com.diyunapp.xilinapp.utils.BitmapUtil;
import com.squareup.picasso.Picasso;
import com.yuyh.library.imgsel.ImageLoader;
import com.yuyh.library.imgsel.ImgSelActivity;
import com.yuyh.library.imgsel.ImgSelConfig;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/18.
 */
public class ReceiveAdapter extends BaseAdapter implements View.OnClickListener{
    private List<ReceiveBean.OrderGoodsBean> list ;
    private Context context;
    private int REQUEST_CODE = 1;
    private String src1=null;
    private String src2=null;
    private String src3=null;
    private  List<String> pathList=null;
    private int pzState=0;
    public ReceiveAdapter(List<ReceiveBean.OrderGoodsBean> list,Context context) {

        this.list = list;
        this.context = context;
    }
    @Override
    public int getCount() {
        return  list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_receive, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ViewHolder viewHolder=holder;
        final ReceiveBean.OrderGoodsBean bean = list.get(position);
//            final ReceiveBean.OrderGoodsBean currentdata = bean.get(position);
        viewHolder.introduceGoods.setText(bean.getGoods_name());
        viewHolder.introducePrice.setText(bean.getGoods_price());

        //rec_id, goods_id, goods_name, goods_price, goods_image
        String image = bean.getGoods_image();
        Picasso.with(context)
                .load(image)
                .resize(200, 200)
                .centerCrop()
                .placeholder(R.drawable.launcher_icon)
                .error(R.drawable.launcher_icon)
                .into(viewHolder.goodsImage);

        viewHolder.edtPingjia.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                bean.setEva_content(s.toString());

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        viewHolder.radiogroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
                if (checkedId == viewHolder.veryGood1.getId()) {
                    bean.setEva_scorel(1);
                } else if (checkedId == viewHolder.good1.getId()) {
                    bean.setEva_scorel(0);
                } else if (checkedId == viewHolder.littleGood1.getId()) {
                    bean.setEva_scorel(-1);
                }
            }
        });
        viewHolder.image1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImgSelActivity.startActivity((Activity) context, config, REQUEST_CODE);
            }
        });

        return convertView;
    }

    @Override
    public void onClick(View v) {

    }
    private ImageLoader loader = new ImageLoader() {
        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            // TODO 在这边可以自定义图片加载库来加载 ImageView，例如 Glide、Picasso、ImageLoader 等
            Glide.with(context).load(path).into(imageView);
        }
    };
    ImgSelConfig config = new ImgSelConfig.Builder(loader)
            // 是否多选
            .multiSelect(true)
            .btnTextColor(Color.WHITE)
            // 使用沉浸式状态栏
            .statusBarColor(Color.parseColor("#eb534a"))
            // 返回图标 ResId
            .backResId(R.drawable.goback)
            // 标题
            .title("图片")
            // 标题文字颜色
            .titleColor(Color.WHITE)
            // TitleBar 背景色
            .titleBgColor(Color.parseColor("#eb534a"))
            .needCamera(true)
            // 最大选择图片数量
            .maxNum(3)
            .build();

    ViewHolder viewHolder;
    //    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     //   super.onActivityResult(requestCode, resultCode, data);
        // 图片选择结果回调
        if (requestCode == REQUEST_CODE && resultCode == -1 && data != null) {
            pathList = data.getStringArrayListExtra(ImgSelActivity.INTENT_RESULT);
            if(pathList.size()==0){
                pzState=0;
            }else{
                pzState=1;
            }
            src1 = null;
            src2 = null;
            src3 = null;

            if (pathList.size() == 1) {
                src1 = pathList.get(0);
                viewHolder.image1.setVisibility(View.VISIBLE);
                Bitmap bitmap = BitmapUtil.createThumbnail(src1,100, 100);
                viewHolder.image1.setImageBitmap(bitmap);

            }else if (pathList.size() == 2) {
                src1 = pathList.get(0);
                src2 = pathList.get(1);
//
                viewHolder.image1.setVisibility(View.VISIBLE);
                viewHolder.image2.setVisibility(View.VISIBLE);

                Bitmap bitmap = BitmapUtil.createThumbnail(src1,100, 100);
                viewHolder.image1.setImageBitmap(bitmap);
                Bitmap bitmap1 = BitmapUtil.createThumbnail(src2,  100, 100);
                viewHolder.image2.setImageBitmap(bitmap1);

            }else if (pathList.size() == 3) {
                src1 = pathList.get(0);
                src2 = pathList.get(1);
                src3 = pathList.get(2);
                viewHolder.image1.setVisibility(View.VISIBLE);
                viewHolder.image2.setVisibility(View.VISIBLE);
                viewHolder.image3.setVisibility(View.VISIBLE);

                Bitmap bitmap = BitmapUtil.createThumbnail(src1, 100, 100);
                viewHolder.image1.setImageBitmap(bitmap);

                Bitmap bitmap1 = BitmapUtil.createThumbnail(src2,  100, 100);
                viewHolder.image2.setImageBitmap(bitmap1);

                Bitmap bitmap2 = BitmapUtil.createThumbnail(src3, 100, 100);
                viewHolder.image3.setImageBitmap(bitmap2);


            }
            Log.i("xiaogaladebug","1---1--"+src1);
            Log.i("xiaogaladebug","2---2--"+src2);
            Log.i("xiaogaladebug","3---3--"+src3);

        }
    }
   static class ViewHolder {
        @Bind(R.id.goods_image)
        ImageView goodsImage;
        @Bind(R.id.introduce_goods)
        TextView introduceGoods;
        @Bind(R.id.introduce_price)
        TextView introducePrice;
        @Bind(R.id.goods_color)
        TextView goodsColor;
        @Bind(R.id.goods_size)
        TextView goodsSize;
        @Bind(R.id.goods_num)
        TextView goodsNum;
        @Bind(R.id.goods_pingfen)
        TextView goodsPingfen;
        @Bind(R.id.star_goods)
        RatingBar starGoods;
        @Bind(R.id.rel_star_goods)
        RelativeLayout relStarGoods;
        @Bind(R.id.very_good1)
        RadioButton veryGood1;
        @Bind(R.id.good1)
        RadioButton good1;
        @Bind(R.id.little_good1)
        RadioButton littleGood1;
        @Bind(R.id.radiogroup1)
        RadioGroup radiogroup1;
        @Bind(R.id.edt_pingjia)
        EditText edtPingjia;
        @Bind(R.id.image_1)
        ImageView image1;
       @Bind(R.id.image_2)
       ImageView image2;
       @Bind(R.id.image_3)
       ImageView image3;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }


   }



}